﻿using System;
using System.IO;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using SQLite;

namespace Timer
{
	[Activity (Label = "Timer", MainLauncher = true, Icon = "@drawable/icon", ScreenOrientation = ScreenOrientation.Portrait)]
	public class MainActivity : Activity
	{
		string bWifi = "true";
		string bWSSync = "true";
		string cURL = "";
		DataManager sqldb;
		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);
			copyAssetDataBase ();
			Button btnEntrar = FindViewById<Button> (Resource.Id.btnEntrar);
			Button btnConfig = FindViewById<Button> (Resource.Id.btnConfig);
			btnEntrar.Click += (object sender, EventArgs e) => { Entrar(sender, e); };
			btnConfig.Click += (object sender, EventArgs e) => { Config(sender, e); };
			btnConfig.Visibility = ViewStates.Invisible;

			bWifi = Intent.GetStringExtra ("bWifi") ?? "true";
			bWSSync = Intent.GetStringExtra ("bWSSync") ?? "true";
			cURL = Intent.GetStringExtra ("cURL") ?? "";
		}

		private void Config(object sender, EventArgs e)
		{
			StartActivity (typeof(Config));
		}

		private void Entrar(object sender, EventArgs e)
		{
			int IdUsuario = -1;
			Button btnConfig = FindViewById<Button> (Resource.Id.btnConfig);
			EditText txtUser = FindViewById<EditText> (Resource.Id.txtUser);
			EditText txtPwd = FindViewById<EditText> (Resource.Id.txtPwd);
			EditText txtLinea = FindViewById<EditText> (Resource.Id.txtLinea);
			EditText txtModelo = FindViewById<EditText> (Resource.Id.txtModelo);
			//login ficticio para habilitar boton de configuracion avanzada
			if (txtUser.Text == "superuser" && txtPwd.Text == "123456")
			{
				btnConfig.Visibility = ViewStates.Visible;
				return;
			}

			sqldb = new DataManager ();
			sqldb.OpenDatabase2("timer_db.db3");
			IdUsuario = sqldb.GetUsuario (txtUser.Text, txtPwd.Text);
			sqldb.CloseDatabase2 ();
			if (IdUsuario != -1)
			{
				//StartActivity (typeof(MainActivity_2));
				var MainActivity2 = new Intent (this, typeof(MainActivity_2));
				MainActivity2.PutExtra ("IdUsuario", IdUsuario);
				MainActivity2.PutExtra ("Clave_de_Acceso", txtUser.Text);
				MainActivity2.PutExtra ("Contrasena", txtPwd.Text);
				MainActivity2.PutExtra ("bWifi", bWifi);
				MainActivity2.PutExtra ("bWSSync", bWSSync);
				MainActivity2.PutExtra ("Linea", txtLinea.Text);
				MainActivity2.PutExtra ("Modelo", txtModelo.Text);
				if (cURL.Length > 0)
					MainActivity2.PutExtra ("cURL", cURL);
				StartActivity (MainActivity2);
			}
			else
			{
				Messagebox ("Error", "Usuario / Contraseña incorrecta");
			}
		}

		public override void OnBackPressed() {
			Intent startMain = new Intent(Intent.ActionMain);
			startMain.AddCategory(Intent.CategoryHome);
			StartActivity(startMain);
		}

		private void copyAssetDataBase()  
		{
			var dbPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "timer_db.db3");

			if (!System.IO.File.Exists (dbPath)) {
				using (var source = Assets.Open (@"timer_db.db3"))
				using (var dest = OpenFileOutput ("timer_db.db3", FileCreationMode.WorldReadable | FileCreationMode.WorldWriteable)) {
					source.CopyTo (dest);
				}
			}
		}

		private void Messagebox(string titulo, string mensaje)
		{
			AlertDialog.Builder builder = new AlertDialog.Builder (this);
			AlertDialog alertDialog = builder.Create ();
			alertDialog.SetTitle (titulo);
			alertDialog.SetIcon (Android.Resource.Drawable.IcDialogAlert);
			alertDialog.SetMessage (mensaje);
			alertDialog.SetButton ("OK", (s, ev) => {
			});
			alertDialog.Show ();
		}
	}
}


